package com.cqw.jackson;

import com.cqw.utils.AESUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-08-25 15:07
 * @Description: id自定义反序列化
 */
@Component
public class IdDeserializer extends JsonDeserializer<Long> {


    /**
     * 自定义反序列化
     *
     * @param p
     * @param ctxt
     * @return
     * @throws IOException
     * @throws JsonProcessingException
     */
    @Override
    public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String text = p.getText().trim();

        if (StringUtils.isBlank(text)) {
            return null;
        }
        return StringUtils.isBlank(AESUtil.decrypt(text, "q1")) ? null : Long.valueOf(AESUtil.decrypt(text, "q1"));
    }
}
